<?php

namespace app\command;

use app\common\command\BaseCommand;
use app\common\model\Store;
use app\model\home\Assistant;
use app\model\home\Duty;
use app\model\home\Room;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\console\input\Option;
use libs\Log;
use think\facade\Debug;

class Migration extends BaseCommand
{
    protected function configure()
    {
        $this->setName('Migration')
            ->addOption('test', 't', Option::VALUE_NONE, 'this is a value_none option')
            ->addOption('required', null, Option::VALUE_REQUIRED, 'this is a value_required option')
            ->addOption('optional', null, Option::VALUE_OPTIONAL, 'this is a value_optional option')
            // 必选参数一定要在可选参数之前
            // ->addArgument('required', Argument::REQUIRED, "argument::required")
            ->addArgument('func', Argument::OPTIONAL, "argument::optional", 'test')
            ->setDescription('this is a description');
    }

    protected function execute(Input $input, Output $output)
    {
        $arguments = array_filter($input->getArguments(true));
        if (empty($arguments)) {
            // return $output->error('please enter $arguments ^_^');
        }
        $options = array_filter($input->getOptions(true));
        if (empty($options)) {
            // return $output->error('please enter options ^_^');
        }
        $func = $input->getArgument('func');
        try {
            // Debug::remark('begin');
            $res = $this->$func();
            // $res && d((array)$res);
            // Debug::remark('end');
        } catch (Exception $ex) {
            return Log::err(__METHOD__, $options, $ex->getMessage());
        }
    }

    public function test()
    {
        //获取助教数据
        $where['status'] = 1;
        $np = Assistant::where($where)
            ->field('store_id,id re_id')->select();
        foreach ($np as $v) {
            $v['re_type'] = 1;
            $v['duty_date'] = date('Y-m-d', time());
        }
        //获取包间数据
        $room = Room::where($where)
            ->field('store_id,id re_id')->select();
        foreach ($room as $v) {
            $v['re_type'] = 2;
            $v['duty_date'] = date('Y-m-d', time());
        }
        $multyArr = array_merge($room->toArray(), $np->toArray());
        $count = count($multyArr);
        $res = Duty::insertAll($multyArr);
        if ($res) {
            echo "批量执行完毕，成功插入{$count}条";
        } else {
            echo "执行失败";
        }
    }
}
